﻿using System;
using System.Web;
using Entidades.Models.Sistema;
using Common;

namespace Web.Handlers
{
    /// <summary>
    /// Summary description for ImageUploadHandler
    /// </summary>
    public class ImageUploadHandler : IGenericHandler
    {
        //public void ProcessRequest(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    context.Response.Write("Hello World");
        //}
        
        /// <summary>
        /// Processes Http Request
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            var respuestaInfo = new RespuestaInfo();
            try
            {
                if ( context.Request.Files.Count > 0 )
                {
                    for(int i=0; i < context.Request.Files.Count; i++)
                    {
                        #region Process file
                        //HttpPostedFile file = context.Request.Files[i];
                        //strFileName = Path.GetFileName(file.FileName);
                        //string strExtension = Path.GetExtension(file.FileName).ToLower();
                        //switch (strExtension)
                        //{
                        //    case ".gif":
                        //        strMIMEType = "image/gif";
                        //        break;

                        //    case ".jpg":
                        //        strMIMEType = "image/jpeg";
                        //        break;

                        //    case ".png":
                        //        strMIMEType = "image/png";
                        //        break;

                        //    case ".mp3":
                        //        strMIMEType = "audio/mp3";
                        //        break;

                        //    default:
                        //        strMIMEType = string.Empty;
                        //        return;
                        //}
                        #endregion

                        //byte[] fileBytes = new byte[file.InputStream.Length];
                        //file.InputStream.Read(fileBytes, 0, fileBytes.Length);

                        byte[] fileBytes = new byte[context.Request.Files[0].InputStream.Length];
                        context.Request.Files[0].InputStream.Read(fileBytes, 0, fileBytes.Length);

                        respuestaInfo.Mensaje   = "Archivo cargado exitosamente.";
                        respuestaInfo.Resultado = true;
                        respuestaInfo.Valor     = Formatter.Base64.ConvertirEnBase64(fileBytes);
                    }
                }
            }
            catch (Exception ex)
            {
                respuestaInfo.Resultado = false;
                respuestaInfo.Mensaje   = ex.Message;
                respuestaInfo.Valor     = null;
            }
            finally
            {
                context.Response.Write(Formatter.JSON<RespuestaInfo>.Serialize(respuestaInfo));
            }
        }

        /// <summary>
        /// Is Reusable?
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}